package com.sq.goodline.util;


import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具
 *
 */
@Slf4j
public class DateUtils {
	
	/**
	 * 传入毫秒数，按指定格式返回
	 * @param time
	 * @param format
	 * @return
	 */
	public static String getDayString(long time,String format) {
		Date d = new Date();
		d.setTime(time);
		return formatDate(d,format);
	}
	
	/**
     * 日期计算，日加减
     *
     * @param date
     *            初始日期
     * @param amount
     *            天数增量（负数为减）
     * @return 计算后的日期
     */
    public static Date addDays(Date date, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, amount);
        return cal.getTime();
    }
    
    /**
     * 日期计算，小时加减
     *
     * @param date
     *            初始日期
     * @param amount
     *            增量（负数为减）
     * @return 计算后的日期
     */
    public static Date addHours(Date date, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, amount);
        return cal.getTime();
    }
    
    /**
     * 按指定格式返回日期字符串
     * @param date
     * @param format  yyyy-MM-dd
     * @return
     */
    public static String formatDate(Date date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }


    /**
     * now
     *
     * @return
     */
    public static String currentDateTime() {
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ldt.format(formatter);
    }

    /**
     * now
     *
     * @return
     */
    public static String currentDateTimeSeq() {
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return ldt.format(formatter);
    }

    /**
     * now
     *
     * @return
     */
    public static String currentDate() {
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return ldt.format(formatter);
    }

    /**
     * string to date
     *
     * @param date
     * @return
     */
    public static Date format(String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(date, formatter);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * string to date
     *
     * @param date
     * @return
     */
    public static Date formatTime(String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }



    /**
     * 将日期按照格式转换成字符串
     * @return
     */
    public static String dateToString(LocalDateTime ldt , String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return ldt.format(formatter);
    }

    public static void main(String[] args) {
        log.info(formatTime("2022-11-17 19:21:33").toString());
    }

}
